import { axios } from "@/plugins/axios"

class FingerReportManual {
  constructor() {
    this.id = undefined
    this.report_info = undefined
  }

  static fetch(query_args = {}, page = 1, per_page = 10) {
    const url = process.env.VUE_APP_API_URL + "api/v1/report_manual"
    let params = { ...query_args, per_page: per_page, page: page }

    return axios.get(url, { params: params }).then((res) => {
      let results = []
      res.data.results.forEach((item) => {
        const data = Object.assign(new FingerReportManual(), item)
        results.push(data)
      })
      return { total: res.data.total, pages: res.data.pages, results: results }
    })
  }

  static async getById(report_id) {
    const res = await FingerReportManual.fetch({ id: report_id }, 1, 0)
    if (res.total > 0) {
      return res.results[0]
    } else {
      return null
    }
  }

  static createNewReport() {
    const handsides = ["left", "right"]
    const fingerindexs = ["拇指", "食指", "中指", "无名指", "小指"]
    const finger_inputs = []
    for (let handside of handsides) {
      for (let finger_index of fingerindexs) {
        finger_inputs.push({
          handside: handside,
          finger_index: finger_index,
          finger_class: null,
          quant_value: 1,
          active_state: null,
          active_order: null,
          order_index: null,
        })
      }
    }

    const report_info = {
      target: "学龄教育",
      user_info: {
        username: null,
        sex: "男",
        mobile: null,
        attrs: {
          birth_date: "2020-01-01",
        },
      },
      finger_inputs: finger_inputs,
      ATD: 0,
      additional_potentail: {
        class_A: "",
        class_B: "",
        class_C: "",
        class_D: "",
        class_E: "",
        class_F: "",
        class_broken: "",
      },
      learn_trait: null,
      learn_type: null,
      natural_properties: []
    }
    let report = new FingerReportManual()
    report.id = null
    report.report_info = report_info
    return report
  }

  async createOrUpdate() {
    const url = process.env.VUE_APP_API_URL + "api/v1/report_manual"
    let data = Object.assign({}, this)

    if (data.id && data.id > 0) {
      const params = { report_id: data.id }
      const res = await axios.put(url, data, { params: params })
      data = res.data.data
    } else {
      const report_info = data.report_info
      const res = await axios.post(url, report_info)
      data = res.data.data
    }
    return Object.assign(new FingerReportManual(), data)
  }

  static createDownloadAction(
    downloadlink,
    filename = "文件下载",
    mimetype = "application/octet-stream"
  ) {
    const link = document.createElement("a")
    link.href = downloadlink
    link.setAttribute("download", filename)
    link.setAttribute("type", mimetype)
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
    link.remove()
  }

  async downloadReport() {
    let url =
      process.env.VUE_APP_API_URL + `api/v1/report_manual/${this.id}/gen`
    const res = await axios.get(url, {
      responseType: "arraybuffer",
      headers: {
        "Content-Type": "application/json",
      },
    })
    const downloadlink = window.URL.createObjectURL(new Blob([res.data]))

    const report_info = this.report_info     
    const natural_properties = report_info.natural_properties.join('+')
    const filename = `${report_info.target}-${report_info.user_info.username}-${natural_properties}.docx`
    const mimetype =
      "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    FingerReportManual.createDownloadAction(downloadlink, filename, mimetype)
  }
}

export { FingerReportManual }